package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domain.business.BusinessListDO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@SuppressWarnings("all")
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional
    public void toCustomer(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.TO_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
        Customer customer = BeanUtil.copyProperties(business,Customer.class);
        customer.setId(null);
        customer.setBusinessId( id);
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        this.customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> businessPool(BusinessPoolDTO dto) {
        dto.setStatus(BusinessStatusConstants.RECOVERY);
        Page pages = new Page<> (dto.getPage(),dto.getPageSize());
        Page<Business> page = this.baseMapper.businessPool(pages,dto);
        PageResult<Business> result = PageResult.<Business>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
        return result;
    }

    //跟进商机
    @Override
    @Transactional
    public void updateBusinessInfoById(BusinessUpdateDTO dto) {
        Business business = BeanUtil.copyProperties(dto, Business.class);
        if(business.getStatus()==BusinessStatusConstants.WAIT_FOLLOW_UP) business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);

        BusinessTrackRecord trackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
        trackRecord.setId(null);
        trackRecord.setUserId(CurrentUserContextHolders.get());
        trackRecord.setCreateTime(LocalDateTime.now());
        trackRecord.setBusinessId(business.getId());
        trackRecord.setKeyItems(dto.getKeyItems().toString());
        this.businessTrackRecordMapper.insert(trackRecord);
    }

    @Override
    public BusinessVO getBusinessById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        BusinessVO vo = BeanUtil.copyProperties(business,BusinessVO.class);
        String assignName = userMapper.selectById(business.getUserId()).getName();
        List<BusinessTrackRecord> trackRecords = businessTrackRecordMapper.selectList(Wrappers.lambdaQuery(BusinessTrackRecord.class)
                .eq(BusinessTrackRecord::getBusinessId,id));
        trackRecords.forEach(trackRecord -> {
            trackRecord.setAssignName(assignName);
        });
        vo.setTrackRecords(trackRecords);
        return vo;
    }

    @Override
    public void addBusiness(Business business) {
        if(ObjectUtil.isEmpty(business.getName())||ObjectUtil.isEmpty(business.getPhone()) ||ObjectUtil.isEmpty(business.getChannel())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        business.setUpdateTime(LocalDateTime.now());
        business.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(business);

    }

    @Override
    public void allocationBusiness(Integer businessId, Integer userId) {
        Business business = this.baseMapper.selectById(businessId);
        business.setUserId(userId);
        business.setStatus(BusinessStatusConstants.WAIT_FOLLOW_UP);
        this.baseMapper.updateById(business);
    }


    @Override
    public PageResult<BusinessListDO> listBusinesses(BusinessListDTO dto) {
        Page pages = new Page<>(dto.getPage(),dto.getPageSize());
//        Page pages = new Page<>(dto.getPage(),dto.getPageSize());
        Page<BusinessListDO> page = this.baseMapper.listBusinesses(pages,dto);
        return PageResult.<BusinessListDO>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }
}
